<?php
require_once('kernel.inc');
include_once(LIBRARIES . '/FastJSON.class.inc');

switch ($event)
{
    case 12:
        if($id != false)
        {
            $vars = $MECCANO->GetProperty($id);
            if($vars != false)
            {
                echo FastJSON::encode($vars);
            } else
            {
                echo 0;
            }
        }
        exit();
}

$ajax_tpl = array(
'news_category' => 'ajax.newscategory.tpl.html',
'content_tree'  => 'ajax.contenttree.tpl.html',
'add_img'=>'ajax.addimage.tpl.html'
);

function Req_tree($lang, $action, $params = false)
{
    $MECCANO = $GLOBALS['MECCANO'];
    $SMARTY = $GLOBALS['SMARTY'];
    $ajax_tpl = $GLOBALS['ajax_tpl'];
    $result = false;
    if($params != false)
    {
        $params = explode(',', $params);
        foreach ($params as $value)
        {
            $var = explode('=', $value);
            $$var[0] = $var[1];
        }
    }

    if($action == 'news_category')
    {
        $category = '';
        $categories = $MECCANO->GetTree(false, 'news', $lang);

        for($i=0, $t=count($categories[0]['branch']); $i<$t; $i++)
        {
            $category[$categories[0]['branch'][$i]['id']] = $categories[0]['branch'][$i]['title'];
        }

        unset($categories);
        if($selected != false)
        {
            isDigit($selected);
            $SMARTY->assign('selected', $selected);
        }

        $SMARTY->assign('category', $category);

        $result = $SMARTY->fetch($ajax_tpl[$action]);
    }

    return $result;

    //return "<option value='' selected>-- Bum! --</option>";
}

if($action == 30)
{
    if($submit === true)
    {
        $SMARTY->assign('make', uploadfile($_FILES['addfile']['name'], $MECCANO->system['contentimg'], $_FILES['addfile']['tmp_name']));
    }

    $SMARTY->display($ajax_tpl['add_img']);
}

if($action == 31)
{
    $img = prepare_str($_REQUEST['img']);

    echo "<html><body><div align='center'>\n";
    echo "<img src='".$MECCANO->system['contentimg']."/".$img."'>";
    echo "</div></body></html>";
}

if($action == 32 || $action == 33)
{
    $img = prepare_str($_REQUEST['img']);
    if($action == 32) @unlink($_SERVER['DOCUMENT_ROOT'].$MECCANO->system['contentimg'].'/'.$img);
    if($action == 33) @unlink($_SERVER['DOCUMENT_ROOT'].$MECCANO->system['newsimg'].'/'.$img);

    echo "<script language='JavaScript'>\n";
    echo "opener.document.location.reload();\n";
    echo "window.close();\n";
    echo "</script>";
}

/*
* ACTIONS:
* 30 - Add image
* 31 - View Image
* 32 - Delete ContentImage
* 33 - Delete News Tumbnail image
*/
?>